//
//  WebViewController.m
//  BasketballClub
//
//  Created by lanou3g on 16/1/9.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "WebViewController.h"
#import "LiveModel.h"

@interface WebViewController ()<UIWebViewDelegate>
@property(nonatomic,strong)LiveModel *liveModel;
///向左轻扫的手势
@property(nonatomic,strong)UISwipeGestureRecognizer *leftSwipeGestureRecognizer;


@end

@implementation WebViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    if ([UIScreen mainScreen].bounds.size.height ==480 ) {
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 32*LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-93*LAYOUT_SIZE_HEIGHT)];
    }else if ([UIScreen mainScreen].bounds.size.height ==568){
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 38*LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-93*LAYOUT_SIZE_HEIGHT)];

    }else{
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 40*LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-93*LAYOUT_SIZE_HEIGHT)];

    }
        [self.view addSubview:self.webView];
    self.webView.delegate = self;
    //webView 加载
    [self loadWebView];
    //webView重新加载
    [self.webView reload];
    //替换导航栏
    [self replaceNAvigationBar];
    //向左轻扫手势的初始化
    self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipesAction:)];
    //指定左手势
    self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;


}

-(void)handleSwipesAction:(UISwipeGestureRecognizer*)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}





#pragma mark - 加载webView
-(void)loadWebView
{
    self.webView.scalesPageToFit  =YES;
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    [self.webView loadRequest:request];
}




#pragma mark 替换导航栏
-(void)replaceNAvigationBar
{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64*LAYOUT_SIZE_HEIGHT)];
    view.backgroundColor =ColorHex(0x0d1c38);
    [self.view addSubview:view];
    
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 - (25 * LAYOUT_SIZE), 29 * LAYOUT_SIZE_HEIGHT, 50* LAYOUT_SIZE, 26 * LAYOUT_SIZE_HEIGHT)];
//    label.text = @"直播";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17*LAYOUT_SIZE];
    
    [view addSubview:label];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10 * LAYOUT_SIZE, 29 * LAYOUT_SIZE_HEIGHT, 50 * LAYOUT_SIZE, 26 * LAYOUT_SIZE_HEIGHT)];

    [button setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    
}
//类似与导航栏左键,button的点击事件
-(void)leftButtonAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark ---去除广告
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('download-tip')[0].style.display = 'none'"];
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('nav')[0].style.display = 'none'"];
}



#pragma mark - 处理内存问题

-(void)dealloc {
    
    self.webView.delegate = nil;
}




@end
